﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import org.aswing.geom.*;

    public class FlowLayout extends EmptyLayout {

        public static const LEFT:int = 2;
        public static const CENTER:int = 0;
        public static const RIGHT:int = 4;

        protected var align:int;
        protected var hgap:int;
        protected var vgap:int;
        protected var margin:Boolean;

        public function FlowLayout(_arg1:int=2, _arg2:int=5, _arg3:int=5, _arg4:Boolean=true){
            this.margin = _arg4;
            this.hgap = _arg2;
            this.vgap = _arg3;
            this.setAlignment(_arg1);
        }
        public function setMargin(_arg1:Boolean):void{
            this.margin = _arg1;
        }
        public function isMargin():Boolean{
            return (this.margin);
        }
        public function getAlignment():int{
            return (this.align);
        }
        public function setAlignment(_arg1:int):void{
            if (((((!((LEFT == LEFT))) && (!((_arg1 == RIGHT))))) && (!((_arg1 == CENTER))))){
                throw (new ArgumentError("Alignment must be LEFT OR RIGHT OR CENTER !"));
            };
            this.align = _arg1;
        }
        public function getHgap():int{
            return (this.hgap);
        }
        public function setHgap(_arg1:int):void{
            this.hgap = _arg1;
        }
        public function getVgap():int{
            return (this.vgap);
        }
        public function setVgap(_arg1:int):void{
            this.vgap = _arg1;
        }
        override public function preferredLayoutSize(_arg1:Container):IntDimension{
            var _local2:IntDimension;
            var _local7:Component;
            var _local8:IntDimension;
            _local2 = new IntDimension(0, 0);
            var _local3:int = _arg1.getComponentCount();
            var _local4:int;
            var _local5:int;
            while (_local5 < _local3) {
                _local7 = _arg1.getComponent(_local5);
                if (_local7.isVisible()){
                    _local8 = _local7.getPreferredSize();
                    _local2.height = Math.max(_local2.height, _local8.height);
                    if (_local4 > 0){
                        _local2.width = (_local2.width + this.hgap);
                    };
                    _local2.width = (_local2.width + _local8.width);
                    _local4++;
                };
                _local5++;
            };
            var _local6:Insets = _arg1.getInsets();
            _local2.width = (_local2.width + (_local6.left + _local6.right));
            _local2.height = (_local2.height + (_local6.top + _local6.bottom));
            if (this.margin){
                _local2.width = (_local2.width + (this.hgap * 2));
                _local2.height = (_local2.height + (this.vgap * 2));
            };
            return (_local2);
        }
        override public function minimumLayoutSize(_arg1:Container):IntDimension{
            return (_arg1.getInsets().getOutsideSize());
        }
        private function moveComponents(_arg1:Container, _arg2:int, _arg3:int, _arg4:int, _arg5:int, _arg6:int, _arg7:int):void{
            var _local9:Component;
            var _local10:IntDimension;
            switch (this.align){
                case LEFT:
                    _arg2 = (_arg2 + 0);
                    break;
                case CENTER:
                    _arg2 = (_arg2 + (_arg4 / 2));
                    break;
                case RIGHT:
                    _arg2 = (_arg2 + _arg4);
                    break;
            };
            var _local8:int = _arg6;
            while (_local8 < _arg7) {
                _local9 = _arg1.getComponent(_local8);
                _local10 = _local9.getSize();
                if (_local9.isVisible()){
                    _local9.setLocation(new IntPoint(_arg2, (_arg3 + ((_arg5 - _local10.height) / 2))));
                    _arg2 = (_arg2 + (_local10.width + this.hgap));
                };
                _local8++;
            };
        }
        override public function layoutContainer(_arg1:Container):void{
            var _local12:Component;
            var _local13:IntDimension;
            var _local2:Insets = _arg1.getInsets();
            var _local3:IntDimension = _arg1.getSize();
            var _local4:int = (this.margin) ? (this.hgap * 2) : 0;
            var _local5:int = (_local3.width - ((_local2.left + _local2.right) + _local4));
            var _local6:int = _arg1.getComponentCount();
            var _local7:int;
            var _local8:int = (_local2.top + (this.margin) ? this.vgap : 0);
            var _local9:int;
            var _local10:int;
            var _local11:int;
            while (_local11 < _local6) {
                _local12 = _arg1.getComponent(_local11);
                if (_local12.isVisible()){
                    _local13 = _local12.getPreferredSize();
                    _local12.setSize(_local13);
                    if ((((_local7 == 0)) || (((_local7 + _local13.width) <= _local5)))){
                        if (_local7 > 0){
                            _local7 = (_local7 + this.hgap);
                        };
                        _local7 = (_local7 + _local13.width);
                        _local9 = Math.max(_local9, _local13.height);
                    } else {
                        this.moveComponents(_arg1, (_local2.left + (this.margin) ? this.hgap : 0), _local8, (_local5 - _local7), _local9, _local10, _local11);
                        _local7 = _local13.width;
                        _local8 = (_local8 + (this.vgap + _local9));
                        _local9 = _local13.height;
                        _local10 = _local11;
                    };
                };
                _local11++;
            };
            this.moveComponents(_arg1, (_local2.left + (this.margin) ? this.hgap : 0), _local8, (_local5 - _local7), _local9, _local10, _local6);
        }
        public function toString():String{
            var _local1 = "";
            switch (this.align){
                case LEFT:
                    _local1 = ",align=left";
                    break;
                case CENTER:
                    _local1 = ",align=center";
                    break;
                case RIGHT:
                    _local1 = ",align=right";
                    break;
            };
            return (((((("FlowLayout[hgap=" + this.hgap) + ",vgap=") + this.vgap) + _local1) + "]"));
        }

    }
}//package org.aswing 
